<?php
/**
 * @file stardefine.php $
 * @author LinLin (linlin@haibao.com) $
 * @date 2016-1-4 下午3:08:14 $
 * @brief  $
 */
namespace haibao\cms\data\star;

use haibao\cms\model\filter\star\StarDefine as StarDefineFilter;
use haibao\cms\model\filter\star\StarBroker as StarBrokerFilter;
use haibao\cms\model\filter\star\StarSecret as StarSecretFilter;
use haibao\cms\model\filter\star\StarProperty as StarPropertyFilter;
use haibao\cms\model\filter\star\StarCooperate as StarCooperateFilter;
use haibao\cms\model\filter\star\StarPlatInfo as StarPlatInfoFilter;
use haibao\cms\business\star\StarDefine as StarDefineBusiness;
use haibao\frame\data\query\Condition as Condition;

class StarDefine extends \haibao\cms\data\BaseMysql{

	public function __construct(){
		parent::__construct('\haibao\cms\model\data\star\StarDefine');
	}

	
	/**
	 * 获取达人关联合作信息和账号信息
	 * @param $starId 达人ID
	 * @param $type 1：合作信息  2：账号信息
	 */
	public function getOneStarCooperateById($starId,$type){
		$starCooperateArr = array();
		if($type == 1){
			$sql = 'select * from StarCooperate where StarId = %s';
			$result = $this->query($sql,array($starId));
			while($row = $result->fetch_assoc()){
				array_push($starCooperateArr,$row);
			}
		}else if($type == 2){
			$sql = 'select * from StarProperty where StarId = %s';
			$result = $this->query($sql,array($starId));
			while($row = $result->fetch_assoc()){
				array_push($starCooperateArr,$row);
			}
		}
		return $starCooperateArr;
	}
	
	public function getAllStarId(){
	    $sql = 'select Id from StarDefine';
	    $result = $this->query($sql);
	    $starIdArr = array();
	    while($row = $result->fetch_assoc()){
	        $starIdArr[] = $row['Id'];
	    }
	    return $starIdArr;
	}
	
	/**
	 * 保存达人信息
	 */
	
	public function saveStarInfo($model,$secretModel,$brokerModel,$starCooperateArray,$starPropertyArray,$newPlatInfoArray,$propertyWModel){
		$this->beginTranscation();
		try {
			if($model->Id){
				$this->save($model);
			}else {
				$this->add($model);
			}
			$this->addStarBroker($model->Id,$brokerModel);
			$this->addStarSecret($model->Id,$secretModel);
			$this->addStarCooperate($model->Id,$starCooperateArray);
			$this->addStarProperty($model->Id,$starPropertyArray);
			$this->addStarPlatInfo($model->Id,$newPlatInfoArray);
			$this->addStarPropertyWInfo($model->Id,$propertyWModel);
		} catch (\Exception $e) {
			$this->rollback();
			throw $e;
		}
		$this->endTranscation();
	}
	
	/**
	 * 根据达人名字获取一条数据
	 */
	public function getOneByRealName($realName,$starId = ''){
		$realName = trim($realName);
		$containType = '';
		if(isset($starId) && $starId){
			$containType = ' and Id != '.$starId;
		}
		$sql = "select RealName from StarDefine where RealName = %s $containType";
		return $this->query($sql,array($realName))->fetch_assoc();
	}
	
	/**
	 * 添加平台相关信息
	 */
	public function addStarPlatInfo($starId,$starPlatInfoArray){
		$this->query('delete from `StarPlatInfo` where StarId = %s', array(
				$starId
		));
		if(!$starId){
			return false;
		}
		
		$sql = 'insert into StarPlatInfo(StarId,PlatType,PlatInfo) values ';
		$arrSql = $arrValues = array();
		foreach($starPlatInfoArray as $key=>$val){
		    array_push($arrSql, '(%s,%s,%s)');
		    array_push($arrValues, $starId);
		    array_push($arrValues, $key);
		    array_push($arrValues, $val);
		}
		if($arrSql && $arrValues){
		    $sql .= implode(',', $arrSql);
		    $this->query($sql, $arrValues);
		}
	}
	
	/**
	 * 添加经济人
	 */
	public function addStarBroker($starId,$brokerModel){
		$this->query('delete from `StarBroker` where StarId = %s limit 1', array(
				$starId
		));
		if(!$starId){
			return false;
		}
		if($brokerModel){
			$brokerModel->StarId = $starId;
			$this->add($brokerModel);
		}
	}
	
	/**
	 * 添加达人保密信息
	 */
	public function addStarSecret($starId,$secretModel){
		$this->query('delete from `StarSecret` where StarId = %s limit 1', array(
				$starId
		));
		if(!$starId){
			return false;
		}

		if($secretModel){
			$secretModel->StarId = $starId;
			$this->add($secretModel);
		}
	}
	
	/**
	 * 添加合作信息
	 */
	public function addStarCooperate($starId,$starCooperateArray){
		$this->query('delete from `StarCooperate` where StarId = %s', array(
				$starId
		));
		if(!$starId){
			return false;
		}
		if(isset($starCooperateArray) && $starCooperateArray){
			foreach($starCooperateArray as $key=>$val){
				$val->StarId = $starId;
			}
			$this->addAll($starCooperateArray);
		}
	}
	
	/**
	 * 添加达人属性
	 */
	public function addStarProperty($starId,$starPropertyArray){
		$this->query('delete from `StarProperty` where StarId = %s', array(
				$starId
		));
		if(!$starId){
			return false;
		}
		if(isset($starPropertyArray) && $starPropertyArray){
			foreach($starPropertyArray as $key=>$val){
				$val->StarId = $starId;
			}
			$this->addAll($starPropertyArray);
		}
	}
	
	/**
	 * 添加达人属性(微信公众号)
	 */
	public function addStarPropertyWInfo($starId,$propertyWModel){
	    $this->query('delete from `StarProperty` where StarId = %s and StarAccountType = %s', array(
	        $starId,\haibao\cms\model\data\star\StarProperty::STAR_ACCOUNT_WEIXIN_PUBLIC
	    ));
	    if(!$starId){
	        return false;
	    }
	    if(isset($propertyWModel) && $propertyWModel){
	        $propertyWModel->StarId = $starId;
	        $this->add($propertyWModel);
	    }
	}
	
	/**
	 * 删除达人
	 */
	public function delStarDefine($id, $status){
		$this->query('update StarDefine set Status=%s where Id=%s limit 1', array(
		    $status, $id
		));
	}
	
	/**
	 * 
	 * 通过多个 id获取所有信息
	 */
	public function getStarByIds($starIds){
		$starArr = array();
		if(empty($starIds)){
			return false;
		}
		$filter = new \haibao\cms\model\filter\star\StarDefine();
		$filter->select(array(
				StarDefineFilter::CONDITION_FIELD_NAME_ID,
				StarDefineFilter::CONDITION_FIELD_NAME_REALNAME,
				StarDefineFilter::CONDITION_FIELD_NAME_NICKNAME,
		));
		$filter->where(StarDefineFilter::CONDITION_FIELD_NAME_ID,\haibao\frame\data\query\Condition::CONDITION_IN,$starIds);
		$filter->where(StarDefineFilter::CONDITION_FIELD_NAME_STATUS, \haibao\frame\data\query\Condition::CONDITION_EQUAL, \haibao\cms\model\data\star\StarDefine::STATUS_ENABLE);
	
		$starList = $this->getAll($filter);
		foreach($starList as $list){
			$starArr[$list->Id] = $list;	 
		}
		return $starArr;
	}
	
	
	public function getStarDefineInfoById($id){
		
		$starDefineFilter = new StarDefineFilter();
		$starDefineFilter->select(array(
				StarDefineFilter::CONDITION_FIELD_NAME_ID,
				StarDefineFilter::CONDITION_FIELD_NAME_IDENTITY_ID,
				StarDefineFilter::CONDITION_FIELD_NAME_AVATAR,
				StarDefineFilter::CONDITION_FIELD_NAME_NICKNAME,
				StarDefineFilter::CONDITION_FIELD_NAME_REALNAME,
				StarDefineFilter::CONDITION_FIELD_NAME_DESCRIPTION,
				StarDefineFilter::CONDITION_FIELD_NAME_CATEGORY_ID,
				StarDefineFilter::CONDITION_FIELD_NAME_PLATFORM,
				StarDefineFilter::CONDITION_FIELD_NAME_COOPERATE_WILLING,
				StarDefineFilter::CONDITION_FIELD_NAME_BRAND_SATISFACTION,
				StarDefineFilter::CONDITION_FIELD_NAME_NOT_COOPERATERANGE,
				StarDefineFilter::CONDITION_FIELD_NAME_REMARKS,
				StarDefineFilter::CONDITION_FIELD_NAME_LOCATION,
		        StarDefineFilter::CONDITION_FIELD_NAME_FRAME_OUT,
		        StarDefineFilter::CONDITION_FIELD_NAME_FRAME_OUT_COST
		));
		$starBrokerFilter = new StarBrokerFilter();
		$starBrokerFilter->select(array(
				StarBrokerFilter::CONDITION_FIELD_NAME_ID,
				StarBrokerFilter::CONDITION_FIELD_NAME_STAR_ID,
				StarBrokerFilter::CONDITION_FIELD_NAME_ADDRESS,
				StarBrokerFilter::CONDITION_FIELD_NAME_EMAIL,
				StarBrokerFilter::CONDITION_FIELD_NAME_LOCATION,
				StarBrokerFilter::CONDITION_FIELD_NAME_MICROBLOG,
				StarBrokerFilter::CONDITION_FIELD_NAME_PHONE,
				StarBrokerFilter::CONDITION_FIELD_NAME_REALNAME,
				StarBrokerFilter::CONDITION_FIELD_NAME_WECHAT,
		));
		$starSecretFilter = new StarSecretFilter();
		$starSecretFilter->select(array(
				StarSecretFilter::CONDITION_FIELD_NAME_ID,
				StarSecretFilter::CONDITION_FIELD_NAME_STAR_ID,
				StarSecretFilter::CONDITION_FIELD_NAME_PHONE,
				StarSecretFilter::CONDITION_FIELD_NAME_WECHAT,
				StarSecretFilter::CONDITION_FIELD_NAME_ADDRESS,
				StarSecretFilter::CONDITION_FIELD_NAME_EMAIL,
		));
		
		$starCooperateFilter = new StarCooperateFilter();
		$starCooperateFilter->select(array(
				StarCooperateFilter::CONDITION_FIELD_NAME_ID,
				StarCooperateFilter::CONDITION_FIELD_NAME_STAR_ID,
				StarCooperateFilter::CONDITION_FIELD_NAME_COOPERATE_BRAND,
				StarCooperateFilter::CONDITION_FIELD_NAME_COOPERATE_CONTENT,
				StarCooperateFilter::CONDITION_FIELD_NAME_COOPERATE_COST,
				StarCooperateFilter::CONDITION_FIELD_NAME_COOPERATE_TIME,
		));
		
		$starPropertyFilter = new StarPropertyFilter();
		$starPropertyFilter->select(array(
				StarPropertyFilter::CONDITION_FIELD_NAME_ID,
				StarPropertyFilter::CONDITION_FIELD_NAME_STAR_ID,
				StarPropertyFilter::CONDITION_FIELD_NAME_FANS_NUM,
				StarPropertyFilter::CONDITION_FIELD_NAME_COOPERATE_MAX_COST,
				StarPropertyFilter::CONDITION_FIELD_NAME_COOPERATE_MIN_COST,
				StarPropertyFilter::CONDITION_FIELD_NAME_STAR_ACCOUNT_TYPE,
				StarPropertyFilter::CONDITION_FIELD_NAME_ACCOUNT_LINK,
				StarPropertyFilter::CONDITION_FIELD_NAME_OTHER_COST,
		        StarPropertyFilter::CONDITION_FIELD_NAME_OTHER_INFO
		));
		
		$starPlatInfoFilter = new StarPlatInfoFilter();
		$starPlatInfoFilter->select(array(
				StarPlatInfoFilter::CONDITION_FIELD_NAME_ID,
				StarPlatInfoFilter::CONDITION_FIELD_NAME_PLAT_INFO,
				StarPlatInfoFilter::CONDITION_FIELD_NAME_PLAT_TYPE,
				StarPlatInfoFilter::CONDITION_FIELD_NAME_STAR_ID,
		));
		
		$starDefineFilter->where(StarDefineFilter::CONDITION_FIELD_NAME_ID, Condition::CONDITION_EQUAL, $id);
		$starDefineFilter->where(StarDefineFilter::CONDITION_FIELD_NAME_STATUS, Condition::CONDITION_EQUAL, \haibao\cms\model\data\star\StarDefine::STATUS_ENABLE);
		$starDefineFilter->leftJoin($starBrokerFilter,null,StarDefineFilter::CONDITION_FIELD_NAME_ID,StarBrokerFilter::CONDITION_FIELD_NAME_STAR_ID,StarDefineFilter::CONDITION_FIELD_NAME_BROKER_NAME,false);
		$starDefineFilter->leftJoin($starSecretFilter,null,StarDefineFilter::CONDITION_FIELD_NAME_ID,StarSecretFilter::CONDITION_FIELD_NAME_STAR_ID,StarDefineFilter::CONDITION_FIELD_NAME_SECRET_INFO,false);
		$starDefineFilter->leftJoin($starCooperateFilter,null,StarDefineFilter::CONDITION_FIELD_NAME_ID,StarCooperateFilter::CONDITION_FIELD_NAME_STAR_ID,StarDefineFilter::CONDITION_FIELD_NAME_COOPERATE_INFO);
		$starDefineFilter->leftJoin($starPropertyFilter,null,StarDefineFilter::CONDITION_FIELD_NAME_ID,StarPropertyFilter::CONDITION_FIELD_NAME_STAR_ID,StarDefineFilter::CONDITION_FIELD_NAME_PROPERTY_INFO);
		$starDefineFilter->leftJoin($starPlatInfoFilter,null,StarDefineFilter::CONDITION_FIELD_NAME_ID,StarPlatInfoFilter::CONDITION_FIELD_NAME_STAR_ID,StarDefineFilter::CONDITION_FIELD_NAME_PLAT_INFO);
		$starDefineInfo = StarDefineBusiness::getOneStarDefine($starDefineFilter);
	
		if(isset($starDefineInfo->Platform) && $starDefineInfo->Platform){
			$starDefineInfo->Platform = explode(',',$starDefineInfo->Platform);
		}
		
// 		echo '<pre>';
// 		print_r($starDefineInfo);
// 		exit;
		return $starDefineInfo;
	}
}